<?php
declare (strict_types=1);

namespace app\common\model;

use app\admin\controller\Theme;
use think\facade\Cache;

class UserMenu extends Base
{
    public function children(): object
    {
        return $this->hasMany(UserMenu::class, 'pid')
            ->with('children');
    }

    protected function getIconAttr($value): string
    {
        if (!empty($value) && !strpos($value, 'uploads') && !strpos($value, 'static')) {
            $theme = Theme::getTheme();
            $value = "/static/api/{$theme['version']}/{$theme['color']}/icon/{$value}";
        }

        return $value;
    }

    public static function getDataList($system, $user): array
    {
        $list = Cache::get('userMenuList');
        if (empty($list)) {
            $field = 'id,title,subtitle,mark,icon,url,count';
            $list = parent::field($field)->where(['pid' => 0, 'status' => 1])
                ->order('sort asc')->select()->toArray();
            $field .= ',total_condition,verify_condition';
            foreach ($list as &$value) {
                $value['children'] = parent::field($field)->where(['pid' => $value['id'], 'status' => 1])
                    ->order('sort asc')->select()->toArray();
            }
            Cache::set('userMenuList', $list);
        }
        $newList = [];
        foreach ($list as &$item) {
            if (!empty($item['url'])) {
                $item['url'] .= strpos($item['url'], '?') ? '&title=' . $item['title'] : '?title=' . $item['title'];
            }
            $children = [];
            foreach ($item['children'] as $child) {
                if (!empty($child['url'])) {
                    $child['url'] .= strpos($child['url'], '?') ? '&title=' . $child['title'] : '?title=' . $child['title'];
                }
                switch ($child['mark']) {
                    case 'order_1':
                    case 'order_2':
                    case 'order_3':
                        if (!empty($user) && !empty($child['total_condition'])) {
                            $child['count'] = Order::where(['uid' => $user->id, 'status' => $child['total_condition']])->count();
                        }
                        $children[] = $child;
                        break;
                    case 'remit':
                    case 'draw':
                    case 'transfer':
                    case 'exchange':
                        if (!empty($child['verify_condition']) && !empty($system[$child['verify_condition']])) {
                            $children[] = $child;
                        }
                        break;
                    case 'invite':
                        if (!empty($user) && $user->is_active == 2) {
                            $children[] = $child;
                        }
                        break;
                    default:
                        $children[] = $child;
                }
            }
            $item['children'] = $children;
            $newList[] = $item;
        }

        return $newList;
    }
}